package utils;

import java.sql.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Constantes {
	public static final String DOCS_PATH = "resources/";
	
	private static final ArrayList<String> STOP_WORDS = new ArrayList<>();
	private static Connection CONNECTION = null;
	public static final String DB_HOST = "peyetraspberry.ddns.net";
	public static final String DB_USERNAME = "web_sem";
	public static final String DB_PASSWORD = "SrMVHa87UYNAqrtH";
	public static final String DB_BASE = "web_sem";
	
	// les deux constantes de pondération d'occurrences dans la formule de Robertson
	public static final double K1 = 2;
	public static final double b = 0.75;
	
	public static ArrayList<String> getStopWords() {
		if (!STOP_WORDS.isEmpty())
			return STOP_WORDS;
		
		try {
			getConnetion();
			
			String requete = "SELECT word FROM stop_word";
			Statement stmt = CONNECTION.createStatement();
			ResultSet resultats = stmt.executeQuery(requete);
			
			ArrayList<String> words = new ArrayList<>();
			
			while (resultats.next()) {
				STOP_WORDS.add(resultats.getString("word"));
			}
			
			return STOP_WORDS;
		} catch (ClassNotFoundException | SQLException ex) {
			Logger.getLogger(Constantes.class.getName()).log(Level.SEVERE, null, ex);
			
			return new ArrayList<String>();
		}
	}
	
	public static synchronized Connection getConnetion() throws ClassNotFoundException, SQLException {
		if (CONNECTION != null)
			return CONNECTION;
		
		String url = "jdbc:mysql://" + DB_HOST + "/" + DB_BASE;
		Class.forName("com.mysql.jdbc.Driver");
		CONNECTION = DriverManager.getConnection(url, DB_USERNAME, DB_PASSWORD);
		
		return CONNECTION;
	}
}
